package com.hlf.modules.sys.service.impl;

import com.hlf.common.constants.Constants;
import com.hlf.common.exception.BusinessException;
import com.hlf.common.utils.DateUtil;
import com.hlf.common.utils.IdWorkerFactory;
import com.hlf.common.utils.ReturnResult;
import com.hlf.modules.merchant.merchant.mapper.MerchantMapper;
import com.hlf.modules.merchant.store.entity.Store;
import com.hlf.modules.merchant.store.mapper.StoreMapper;
import com.hlf.modules.sys.entity.SoftOrder;
import com.hlf.modules.sys.entity.SoftService;
import com.hlf.modules.sys.entity.SysUserEntity;
import com.hlf.modules.sys.mapper.SoftOrderMapper;
import com.hlf.modules.sys.mapper.SoftServiceMapper;
import com.hlf.modules.sys.service.SoftwareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

/**
 * Created by Administrator on 2017/10/31.
 */

@Service("softwareService")
public class SoftwareServiceImpl implements SoftwareService {

    @Autowired
    private SoftOrderMapper softOrderMapper;
    @Autowired
    private SoftServiceMapper softServiceMapper;
    @Autowired
    private MerchantMapper merchantMapper;
    @Autowired
    private StoreMapper storeMapper;

    /**
     * 服务商详情
     *
     * @return
     */
    @Override
    public SoftService getInfo() {
        SoftService softService = softServiceMapper.selectByPrimaryKey(1);
        return softService;
    }

    /**
     * 购买服务
     *
     * @param user
     * @param serviceYearNum
     * @return
     */
    @Override
    @Transactional
    public ReturnResult buy(SysUserEntity user, Integer serviceYearNum) {
        Long userId = user.getUserId();
        long storeId = Long.parseLong(user.getCurrentStoreId());
        Store store = storeMapper.queryObject(storeId);
        String storeName = store.getName();
        Long merchantId = store.getMerchantId();
        String merchantName = store.getMerchantName();
        SoftService info = getInfo();
        Integer firstYearPrice = info.getFirstYearPrice();
        Integer nextYearPrice = info.getNextYearPrice();
        SoftOrder softOrder = new SoftOrder();

        Byte softwareStatus = store.getSoftwareStatus();
        Date expireDate = store.getExpiryDate();
        if (Constants.SOFTWARE_PAY_STATUS_PAY.equals(softwareStatus)) {
            expireDate = DateUtil.addYear(expireDate, serviceYearNum.intValue());
        } else {
            //过期时间
            expireDate = DateUtil.addYear(new Date(), serviceYearNum.intValue());
        }

        softOrder.setId(IdWorkerFactory.nextId());
        softOrder.setMerchantId(merchantId);
        softOrder.setMerchantName(merchantName);
        softOrder.setStoreId(storeId);
        softOrder.setStoreName(storeName);
        if (serviceYearNum <= 0) {
            throw new BusinessException("请输入正确年限!");
        } else if (Constants.SOFTWARE_PAY_STATUS_PAY_EXPIRE.equals(softwareStatus) ||
                Constants.SOFTWARE_PAY_STATUS_PAY.equals(softwareStatus)) {
            if (Constants.SOFTWARE_PAY_STATUS_PAY_EXPIRE.equals(softwareStatus)){
                softOrder.setOrderType(Constants.SOFT_ORDER_NEW_QUIT_RENEW);
            }else {
                softOrder.setOrderType(Constants.SOFT_ORDER_TYPE_USE_RENEW);
            }
            BigDecimal next = new BigDecimal(nextYearPrice);
            BigDecimal totalAmt;
            totalAmt = next.multiply(BigDecimal.valueOf(serviceYearNum));
            softOrder.setTotalAmt(totalAmt.intValue());
            softOrder.setPayAmt(totalAmt.intValue());
        } else if (serviceYearNum == 1) {
            softOrder.setTotalAmt(firstYearPrice);
            softOrder.setPayAmt(firstYearPrice);
        } else {
            softOrder.setOrderType(Constants.SOFT_ORDER_TYPE_NEW);
            BigDecimal first = new BigDecimal(firstYearPrice);
            BigDecimal next = new BigDecimal(nextYearPrice);
            BigDecimal totalAmt;
            totalAmt = next.multiply(BigDecimal.valueOf(serviceYearNum - 1));
            totalAmt = totalAmt.add(first);
            softOrder.setTotalAmt(totalAmt.intValue());
            softOrder.setPayAmt(totalAmt.intValue());
        }
        softOrder.setServiceYearNum(serviceYearNum);
        softOrder.setSoftOrderNum(IdWorkerFactory.nextId());
        softOrder.setPayStatus(Constants.ORDER_PAY_STATUS_UNPAID);
        softOrder.setRefundStatus(Constants.ORDER_REFUND_STATUS_NO);
        softOrder.setExpireDate(expireDate);
        softOrder.setInsertBy(userId);
        softOrder.setInsertDate(new Date());
        softOrder.setLastUpdateDate(new Date());

        int i = softOrderMapper.insert(softOrder);
//        store.setExpiryDate(expireDate);
//        store.setSoftwareStatus(Constants.SOFTWARE_PAY_STATUS_PAY);
//        int j = storeMapper.updateByPrimaryKeySelective(store);
        if (i == 0) {
            throw new BusinessException("下单失败!");
        }
        return ReturnResult.ok();
    }

    /**
     * 删除未完成订单
     * @param user
     */
    @Override
    public void deleteUnfinish(SysUserEntity user) {
        Long storeId = Long.parseLong(user.getCurrentStoreId());
        softOrderMapper.deleteByStoreIdAndStatus(storeId,Constants.ORDER_PAY_STATUS_UNPAID);

    }


}
